package com.corner.presenter.imp;

import com.amap.api.location.AMapLocation;
import com.corner.app.App;
import com.corner.model.cornerservice.domain.GeoPoint;
import com.corner.model.cornerservice.domain.Info;
import com.corner.model.cornerservice.domain.Location;
import com.corner.model.cornerservice.helper.InfoHelper;
import com.corner.model.cornerservice.helper.LocationHelper;
import com.corner.presenter.ICreateInfoPresenter;
import com.corner.presenter.base.BasePresenter;
import com.corner.ui.parcel.PoiItem;
import com.corner.ui.view.ICreateInfoView;
import com.corner.utils.LocationUtil;
import com.corner.utils.NetUtil;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by Administrator on 2015/7/23，16.11.
 * 描述：
 */
public class CreateInfoPresenter extends BasePresenter<ICreateInfoView> implements ICreateInfoPresenter {
    @Override
    public void requestLocate() {
        LocationUtil.getInstance().startLocation();
        LocationUtil.getInstance().getLocation(new LocationUtil.OnLocationGetListener() {
            @Override
            public void onLocationGet(AMapLocation location) {
                view.setLocationName(location.getCity());
            }
        });
    }

    @Override
    public void sendInfo(final Info info, boolean isReSend) {
        view.showSending(info, isReSend);
        if (NetUtil.checkNet(App.application)) {
            LocationUtil.getInstance().startLocation();
            LocationUtil.getInstance().getLocation(new LocationUtil.OnLocationGetListener() {
                @Override
                public void onLocationGet(final AMapLocation aMapLocation) {
                    final GeoPoint geoPoint = new GeoPoint(aMapLocation.getLatitude(), aMapLocation.getLongitude());
                    info.setGeoPoint(geoPoint);
                    final String city = aMapLocation.getCity();
                    LocationHelper.getLocationByName(city, geoPoint).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Location>() {
                        @Override
                        public void call(Location location) {
                            if (location != null) setAreaAndSend(location, info);
                            else {
                                location = new Location();
                                location.setGeoPoint(geoPoint);
                                location.setLocationName(city);
                                location.setAddress(aMapLocation.getAddress());
                                location.setProvince(aMapLocation.getProvince());
                                location.setCity(aMapLocation.getCity());
                                LocationHelper.saveLocation(location).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Location>() {
                                    @Override
                                    public void call(Location location) {
                                        setAreaAndSend(location, info);
                                    }
                                }, new Action1<Throwable>() {
                                    @Override
                                    public void call(Throwable throwable) {
                                        if (view != null)
                                            view.showSendError(info);
                                    }
                                });
                            }
                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                            if (view != null)
                                view.showSendError(info);
                        }
                    });
                }
            });
        } else {
            if (view != null)
                view.showSendError(info);
        }
    }

    @Override
    public void sendInfo(final Info info, final PoiItem item, boolean isReSend) {
        view.showSending(info, isReSend);
        if (NetUtil.checkNet(App.application)) {

            LocationUtil.getInstance().startLocation();
            LocationUtil.getInstance().getLocation(new LocationUtil.OnLocationGetListener() {
                @Override
                public void onLocationGet(AMapLocation aMapLocation) {
                    final GeoPoint geoPoint = new GeoPoint(aMapLocation.getLatitude(), aMapLocation.getLongitude());
                    info.setGeoPoint(geoPoint);
                    LocationHelper.getLocationByName(item.name, geoPoint).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Location>() {
                        @Override
                        public void call(Location location) {
                            if (location != null)
                                setAreaAndSend(location, info);
                            else {
                                location = new Location();
                                location.setGeoPoint(new GeoPoint(item.latitude,item.longtitude));
                                location.setLocationName(item.name);
                                location.setAddress(item.address);
                                location.setProvince(item.provice);
                                location.setCity(item.city);
                                LocationHelper.saveLocation(location).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Location>() {
                                    @Override
                                    public void call(Location location) {
                                        setAreaAndSend(location, info);
                                    }
                                }, new Action1<Throwable>() {
                                    @Override
                                    public void call(Throwable throwable) {
                                        if (view != null)
                                            view.showSendError(info);
                                    }
                                });
                            }
                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                            if (view != null)
                                view.showSendError(info);
                        }
                    });
                }
            });
        } else {
            if (view != null)
                view.showSendError(info);
        }
    }

    @Override
    public void sendInfo(final Info info, final String locationName, boolean isReSend) {
        view.showSending(info, isReSend);
        if (NetUtil.checkNet(App.application)) {

            LocationUtil.getInstance().startLocation();
            LocationUtil.getInstance().getLocation(new LocationUtil.OnLocationGetListener() {
                @Override
                public void onLocationGet(final AMapLocation aMapLocation) {
                    final GeoPoint geoPoint = new GeoPoint(aMapLocation.getLatitude(), aMapLocation.getLongitude());
                    info.setGeoPoint(geoPoint);
                    LocationHelper.getLocationByName(locationName, geoPoint).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Location>() {
                        @Override
                        public void call(Location location) {
                            if (location != null)
                                setAreaAndSend(location, info);
                            else {
                                location = new Location();
                                location.setGeoPoint(geoPoint);
                                location.setLocationName(locationName);
                                location.setAddress(aMapLocation.getAddress());
                                location.setProvince(aMapLocation.getProvince());
                                location.setCity(aMapLocation.getCity());
                                LocationHelper.saveLocation(location).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Location>() {
                                    @Override
                                    public void call(Location location) {
                                        setAreaAndSend(location, info);
                                    }
                                }, new Action1<Throwable>() {
                                    @Override
                                    public void call(Throwable throwable) {
                                        if (view != null)
                                            view.showSendError(info);
                                    }
                                });
                            }
                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                            if (view != null)
                                view.showSendError(info);
                        }
                    });
                }
            });
        } else {
            if (view != null)
                view.showSendError(info);
        }
    }

    /**
     * 设置区域并发送
     *
     * @param location
     * @param info
     */
    private void setAreaAndSend(Location location, final Info info) {
        info.setArea(location);
        InfoHelper.sendInfoIntoInfoTask(info).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Info>() {

            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                if (view != null)
                    view.showSendError(info);
            }

            @Override
            public void onNext(Info info) {
                if (view != null)
                    view.sendSucess(info);
            }
        });
    }
}
